package cn.kgc.controller;

import cn.kgc.pojo.Room;
import cn.kgc.pojo.RoomType;
import cn.kgc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李梦煊
 * @version 1.0
 * @date 2022/10/7 - 14:08
 */
@Controller
public class RoomController {
    @Autowired
    private RoomService roomService;

    @RequestMapping("/getRoomByName")
    @ResponseBody
    public Map<String, Object> getRoomByName(String page, String limit, String typeName) {
        HashMap<String, Object> map = new HashMap<>();
        int crrPage = 1;
        if (page != null && !"".equals(page)) {
            crrPage = Integer.parseInt(page);
        }
        int lim = Integer.parseInt(limit);
        crrPage = (crrPage - 1) * lim;

        List<Room> room = roomService.getRoomByName(crrPage, lim, typeName);

        //得到总页数
        int totalPage = roomService.getTotalCount(typeName);
        map.put("count", totalPage);
        map.put("data", room);
        map.put("code", 0);
        map.put("msg", "");

        return map;
    }


    //新增房间
    @RequestMapping("/addRoom")
    @ResponseBody
    public String addRoom(Integer roomNum,
                          String typeName,
                          Integer typeId,
                          Double roomPrice,
                          String roomState,
                          Date startTime,
                          Date deadTime) {

        System.out.println("进来了");
        System.out.println("typeName=="+typeName);

        String[] split = typeName.split(",");

        String Id = split[0];
        typeId = Integer.parseInt(Id);
        typeName= split[1];

        Room room =new Room();
        room.setRoomNum(roomNum);
        room.setTypeName(typeName);
        room.setTypeId(typeId);
        room.setRoomPrice(roomPrice);
        room.setRoomState(roomState);
        room.setStartTime(startTime);
        room.setDeadTime(deadTime);
        System.out.println(room);

        roomService.addRoom(room);
        return "findRoom";
    }


    //删除
    @RequestMapping("/deleRoomById")
    @ResponseBody
    public String deleRoomById(String roomId){
        roomService.deleRoomById(roomId);
        return "findRoom";
    }


    //修改
    @RequestMapping("/updateRoom")
    @ResponseBody
    public String updateRoom(
            Integer roomId,
            Integer roomNum,
            String typeName,
            Integer typeId,
            Double roomPrice,
            String roomState,
            Date startTime,
            Date deadTime
    ){
        System.out.println("typeName==="+typeName);

        String[] split = typeName.split(",");

        String Id = split[0];
        typeId = Integer.parseInt(Id);
        typeName = split[1];

        Room room =new Room();
        room.setRoomId(roomId);
        room.setRoomNum(roomNum);
        room.setTypeName(typeName);
        room.setTypeId(typeId);
        room.setRoomPrice(roomPrice);
        room.setRoomState(roomState);
        room.setStartTime(startTime);
        room.setDeadTime(deadTime);
        System.out.println("room=="+room);

        int i = roomService.updateRoom(room);
        System.out.println("i=="+i);
        return "findRoom";
    }



    @GetMapping("room/{typeId}")
    public String findBookById(@PathVariable("typeId") Integer typeId, Model model) {
        RoomType room = roomService.findRoomById(typeId);
        model.addAttribute("room", room);
        return "user/xq";
    }


    //新增订单详情
    @RequestMapping("/addOrders")
    @ResponseBody
    public Map<String, Object> addOrders(String typeName){
        HashMap<String, Object> maps = new HashMap<>();

        List<Room> room = roomService.addOrders(typeName);

        maps.put("data", room);
        maps.put("msg", "");

        return maps;

    }
}
